home *** CD-ROM | disk | FTP | other *** search
/ 3D Games - Real-time Rend…ng & Software Technology / 3D Games - Real-time Rendering & Software Technology.iso / flysdk / util / q2md2fly / q2md2fly.h < prev   
Encoding:
C/C++ Source or Header  |  2000-01-06  |  4.3 KB  |  205 lines

  1. /*
  2. ========================================================================
  3.  
  4. .MD2 triangle model file format
  5.  
  6. ========================================================================
  7. */
  8.  
  9. #define IDALIASHEADER        (('2'<<24)+('P'<<16)+('D'<<8)+'I')
  10. #define ALIAS_VERSION    8
  11.  
  12. #define    MAX_TRIANGLES    4096
  13. #define MAX_VERTS        2048
  14. #define MAX_FRAMES        512
  15. #define MAX_MD2SKINS    32
  16. #define    MAX_SKINNAME    64
  17.  
  18. typedef struct
  19. {
  20.     short    s;
  21.     short    t;
  22. } dstvert_t;
  23.  
  24. typedef struct 
  25. {
  26.     short    index_xyz[3];
  27.     short    index_st[3];
  28. } dtriangle_t;
  29.  
  30. typedef struct
  31. {
  32.     byte    v[3];            // scaled byte to fit in frame mins/maxs
  33.     byte    lightnormalindex;
  34. } dtrivertx_t;
  35.  
  36. typedef struct
  37. {
  38.     float u,v;
  39.     int index_vert;
  40. } dcomandvertex_t;
  41.  
  42. #define DTRIVERTX_V0   0
  43. #define DTRIVERTX_V1   1
  44. #define DTRIVERTX_V2   2
  45. #define DTRIVERTX_LNI  3
  46. #define DTRIVERTX_SIZE 4
  47.  
  48. typedef struct
  49. {
  50.     float        scale[3];    // multiply byte verts by this
  51.     float        translate[3];    // then add this
  52.     char        name[16];    // frame name from grabbing
  53.     dtrivertx_t    verts[1];    // variable sized
  54. } daliasframe_t;
  55.  
  56.  
  57. // the glcmd format:
  58. // a positive integer starts a tristrip command, followed by that many
  59. // vertex structures.
  60. // a negative integer starts a trifan command, followed by -x vertexes
  61. // a zero indicates the end of the command list.
  62. // a vertex consists of a floating point s, a floating point t,
  63. // and an integer vertex index.
  64.  
  65.  
  66. typedef struct
  67. {
  68.     int            ident;
  69.     int            version;
  70.  
  71.     int            skinwidth;
  72.     int            skinheight;
  73.     int            framesize;        // byte size of each frame
  74.  
  75.     int            num_skins;
  76.     int            num_xyz;
  77.     int            num_st;            // greater than num_xyz for seams
  78.     int            num_tris;
  79.     int            num_glcmds;        // dwords in strip/fan command list
  80.     int            num_frames;
  81.  
  82.     int            ofs_skins;        // each skin is a MAX_SKINNAME string
  83.     int            ofs_st;            // byte offset from start for stverts
  84.     int            ofs_tris;        // offset for dtriangles
  85.     int            ofs_frames;        // offset for first frame
  86.     int            ofs_glcmds;    
  87.     int            ofs_end;        // end of file
  88.  
  89. } dmdl_t;
  90.  
  91.  
  92.  
  93. #define PiOver180        1.74532925199433E-002f
  94.  
  95. class vector
  96. {
  97.     public:
  98.         float x,y,z;
  99.  
  100.     vector() 
  101.     { ; };
  102.     inline vector(float x0,float y0,float z0)
  103.     { x=x0; y=y0; z=z0; };
  104.     vector(vector &v) 
  105.     { *this=v; };
  106.     inline void null(void)
  107.     { x=y=z=0; };
  108.     inline float length(void)
  109.     { return (float)sqrt(x*x+y*y+z*z); };
  110.     inline void  vec(float x0,float y0,float z0)
  111.     { x=x0; y=y0; z=z0; };
  112.     inline void  negate(void)
  113.     { x=-x; y=-y; z=-z; };
  114.     inline void cross(vector& v1,vector& v2)
  115.     {
  116.      x=v1.y*v2.z-v1.z*v2.y;
  117.      y=v1.z*v2.x-v1.x*v2.z;
  118.      z=v1.x*v2.y-v1.y*v2.x;
  119.     }
  120.     inline int normalize(void)
  121.     {
  122.      float len=(float)sqrt(x*x+y*y+z*z);
  123.      if (len<0.0f)
  124.         return 0;
  125.      x/=len; y/=len; z/=len;
  126.      return 1;
  127.     }
  128.     inline float operator[](int i) { return (&x)[i]; };
  129. };
  130.  
  131. class mat4x4
  132. {
  133. public:
  134.     float m[4][4];
  135.  
  136.     inline void null(void)
  137.     {
  138.         memset(&m,0,sizeof(m));
  139.     }
  140.     inline void load_identity(void)
  141.     {
  142.          memset(m,0,sizeof(m));
  143.          m[0][0]=m[1][1]=m[2][2]=m[3][3]=1.0;
  144.     }
  145.  
  146.     void set_rotation( vector& dir, float rad )
  147.     {
  148.         static mat4x4 m;
  149.  
  150.         rad*=-PiOver180;
  151.  
  152.         float fCos=(float)cos( rad );
  153.         float fSin=(float)sin( rad );
  154.  
  155.         m.m[0][0] = ( dir.x * dir.x ) * ( 1 - fCos ) + fCos;
  156.         m.m[0][1] = ( dir.x * dir.y ) * ( 1 - fCos ) - (dir.z * fSin);
  157.         m.m[0][2] = ( dir.x * dir.z ) * ( 1 - fCos ) + (dir.y * fSin);
  158.  
  159.         m.m[1][0] = ( dir.y * dir.x ) * ( 1 - fCos ) + (dir.z * fSin);
  160.         m.m[1][1] = ( dir.y * dir.y ) * ( 1 - fCos ) + fCos ;
  161.         m.m[1][2] = ( dir.y * dir.z ) * ( 1 - fCos ) - (dir.x * fSin);
  162.  
  163.         m.m[2][0] = ( dir.z * dir.x ) * ( 1 - fCos ) - (dir.y * fSin);
  164.         m.m[2][1] = ( dir.z * dir.y ) * ( 1 - fCos ) + (dir.x * fSin);
  165.         m.m[2][2] = ( dir.z * dir.z ) * ( 1 - fCos ) + fCos;
  166.     
  167.         m.m[0][3] = m.m[1][3] = m.m[2][3] = 0;
  168.         m.m[3][1] = m.m[3][2] = m.m[3][3] = 0;
  169.         m.m[3][3] = 1;
  170.  
  171.         *this=*this*m;
  172.     }
  173.  
  174.     inline mat4x4 operator*(mat4x4& m1)
  175.     {
  176.       int i,j,k;
  177.       float ab;
  178.       mat4x4 m2;
  179.  
  180.       for(i=0; i<4; i++)
  181.         for(j=0; j<4; j++)
  182.         {
  183.           ab=0;
  184.           for(k=0; k<4; k++)
  185.                ab+=m[i][k]*m1.m[k][j];
  186.           m2.m[i][j]=ab;
  187.         }
  188.       return m2;
  189.     };
  190. };
  191.  
  192. inline vector operator*(vector& v,mat4x4& m)
  193. {
  194.   vector a;
  195.   float *f=&a.x;
  196.   for(int j=0; j<3; j++,f++ )
  197.   {
  198.     *f=0;
  199.     for(int i=0; i<3; i++)
  200.       *f+=*(&v.x+i)*m.m[i][j];
  201.     *f+=m.m[i][j];
  202.   }
  203.   return a;
  204. }
  205.